package com.injurydetection.www.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

import androidx.annotation.Nullable;

import com.injurydetection.www.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sunguiyong on 2020/8/10
 */
public class PainView extends View {
    private Paint mPaint;
    private List<Float> dataList;
    private int mWidth;
    private int mHeight;
    private float putx;

    public PainView(Context context) {
        super(context);
        dataList = new ArrayList<>();
    }

    public PainView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        dataList = new ArrayList<>();
        DisplayMetrics dm = new DisplayMetrics();
        dm = getResources().getDisplayMetrics();
        int dpi = dm.densityDpi;
        mWidth = (int) ((dpi / 25.4) * 5);
        mHeight = (int) ((dpi / 25.4) * 1.6);
        putx = (float) ((dpi / 25.4) * 30 / 200);
    }

    public PainView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        dataList = new ArrayList<>();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float height = getHeight();
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(3f);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        List<Float> datas = new ArrayList<>();
        if (dataList.size() > 1) {
            for (int i = 0; i < dataList.size(); i++) {
                datas.add(((height / 17) * ((220 - dataList.get(i)) / 10)));
            }
        }
        for (int i = 0; i < datas.size(); i++) {
            canvas.drawLine(7 * mWidth - (datas.size() - i) * putx, datas.get(i - 1),
                    7 * mWidth - (datas.size() - i - 1) * putx,
                    datas.get(i), mPaint);
        }
    }
    public void setData(ArrayList<Float> data){
        dataList=data;
        invalidate();
    }
}
